﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.home
{
    public partial class BlogList_Friend : System.Web.UI.Page
    {
        int userID = 9;//AppCode.TestUserInfo._id;//暂用用户ID，写死，到时候用COOKIE替换过来
        protected List<int?> count;
        protected void Page_Load(object sender, EventArgs e)
        {

            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            int currentpage = 1;//当前页
            int pagesize = 4;//页记录大小

            //把被查询页赋值给当前页
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);
            }

            StringBuilder indexHtml = new StringBuilder(Cache["user_bloglist_Friend"].ToString());
            //头部、底部、左侧用户命令菜单
            indexHtml = indexHtml.Replace("$Header$", Cache["user_header2"].ToString());
            indexHtml = indexHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            indexHtml = indexHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            indexHtml = indexHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());

            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            //BLL.User_LetterInbox bllInbox = new Jy123.BLL.User_LetterInbox();
            //bllInbox.GetList("FUser = '" + userID + "'");
            indexHtml = indexHtml.Replace("$NewMsg$", newLetterCount.ToString());

            //好友的日志列表
            Jy123.BLL.User.Blog bllUserBlog = new Jy123.BLL.User.Blog();
            DataTable dtBlog = bllUserBlog.BlogList_MyFriend(currentpage, pagesize, userID, out count).Tables[0];
            StringBuilder sbBlogLit = new StringBuilder();
            for (int i = 0; i < dtBlog.Rows.Count; i++)
            {
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtBlog.Rows[i]["FUser"]));

                string strHtml = Cache["user_BlogList_Friend_div"].ToString();
                strHtml = strHtml.Replace("$ID$", dtBlog.Rows[i]["ID"].ToString());
                strHtml = strHtml.Replace("$FUser$",dtBlog.Rows[i]["FUser"].ToString());
                strHtml = strHtml.Replace("$UserName$", dtBlog.Rows[i]["UserName"].ToString());
                strHtml = strHtml.Replace("$Portrait$", userPortrait);
                strHtml = strHtml.Replace("$PostTime$", dtBlog.Rows[i]["PostTime"].ToString());
                strHtml = strHtml.Replace("$Title$", Common.Input.GetSubString(dtBlog.Rows[i]["Title"].ToString(),40));//截取标题为40个字符
                //截取内容为60个字符并去除HTML标签
                strHtml = strHtml.Replace("$BlogContent$", Common.Input.GetSubString(Common.Input.LostHTML(dtBlog.Rows[i]["BlogContent"].ToString()), 60));
                strHtml = strHtml.Replace("$Reads$", dtBlog.Rows[i]["Reads"].ToString());
                strHtml = strHtml.Replace("$Comments$", dtBlog.Rows[i]["Comments"].ToString());
                sbBlogLit.Append(strHtml);
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            string strHref = "BlogList_Friend.aspx?page=$num$";
            MoonPage mp = new MoonPage(false, true);
            mp.Count = Convert.ToInt32(count[1]);
            sbBlogLit.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            
            indexHtml.Replace("$BlogList_Friend_div$", sbBlogLit.ToString());

            Response.Write(indexHtml.ToString());
        }
    }
}
